package com.momo.web.controller.authority;

import com.google.common.collect.Maps;
import com.momo.common.common.JSONResult;
import com.momo.common.res.authority.LoginAuthReq;
import com.momo.service.async.InitAclToRedisAsync;
import com.momo.service.authority.AdminAuthorityService;
import com.momo.web.controller.BaseController;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.Map;

/**
 * Created by MOMO on 2019/3/22.
 * 授权管理中心
 */
@RestController
@RequestMapping(value = "/platform/authority", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
@Slf4j
public class AuthorityController extends BaseController {
    @Autowired
    private InitAclToRedisAsync initAclToRedisAsync;
    @Autowired
    private AdminAuthorityService authorityService;

    /**
     * 异步  初始化权限到redis
     * @return
     */
    @RequestMapping("/initAclToRedis/v1")
    public JSONResult initAclToRedis() {
        return initAclToRedisAsync.initAclToRedis();
    }

    /**
     * 动态权限菜单
     *
     * @return
     */
    @RequestMapping("/dynamicMenu/v1")
    public JSONResult dynamicMenu(@Validated(LoginAuthReq.Permission.class)@RequestBody LoginAuthReq loginAuthReq) {
        try {
            Map<String, Object> map = Maps.newHashMap();
            map.put("acls", authorityService.userAclTree(loginAuthReq, redisUser()));
//            map.put("roles", sysRoleService.getRoleListByUserId(userId));
            return JSONResult.ok(map);
        } catch (UnsupportedEncodingException e) {
            log.error("获取动态权限菜单 失败:{}", e.getMessage());
            return JSONResult.errorMap("获取动态权限菜单 失败");
        }
    }
}
